package com.changgou.goods.init;

import com.changgou.goods.service.BrandService;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.service.SpecService;
import com.changgou.pojo.Category;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class Init implements InitializingBean {

    @Autowired
    private BrandService brandService;

    @Autowired
    private SpecService specService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    private final String brandKey="BRAND_LIST";
    private final String specKey="SPEC_LIST";

    @Override
    public void afterPropertiesSet() throws Exception {
        //缓存预热
        System.out.println("开始缓存预热");
        List<Category> categoryList = categoryService.findAll();//查询商品分类列表
        for(Category category:categoryList){
            //根据商品分类查询品牌列表
            List<Map> brandList = brandService.findListByCategoryName( category.getName() );
            redisTemplate.boundHashOps( brandKey ).put( category.getName(),brandList );
            //根据商品分类查询规格列表
            List<Map> specList = specService.findListByCategoryName( category.getName() );
            redisTemplate.boundHashOps( specKey ).put( category.getName(),specList );
        }
        System.out.println("缓存预热结束");
    }
}
